<?php
if (!defined("LEVEL") || LEVEL < 30 || !defined("OK")) { header('location: ?home'); }

// biski kintamuju

    $host = "?id,46;a,8"; // pagrindinis
    $denny = "conf.php|localhost.php";
    // knopkes
    $img_back="images/admin/back.gif";
    $img_folder="images/admin/folder.gif";
    $img_file="images/admin/file.gif";
    $img_home="images/admin/home.gif";
    $img_surce="images/icons/page_white_code_red.png";


if (isset($url['v'])) {
	$denny = explode("|", $denny);
	if (!in_array($url['v'], $denny) && is_file($url['d'].$url['v'])) {
		$h = "<table bgcolor=#EEEEEE><tr><td width=30 valign='top'><code>";
		for ($i = 1; $i <= count(file($url['d'].$url['v'])); $i++) $h .= $i.".<br>";
		$h .= "</code></td><td>";
		$h .= highlight_file($url['d'].$url['v'], true);
		$h .= "</td></tr></table>";
		hide($url['v']." - failo turinys",$h);
		unset($h);
	}
	else { klaida("Draudziama!","Konfidenciali informacija!"); }
}

function s($arr, $str) {
   if (is_array($arr)) {
       $ilgis = strlen($str); $return = '';
       foreach ($arr as $key => $val) {
           $tmp = substr($val, 0, $ilgis);
           if ($str == $tmp) {
               $return .= $val."\n";
           }
       }
       return $return;
   }
   return false;
}



// grazina failo pletini
function strip_ext($name,$ext='') {
         $ext = substr($name, strlen($ext)-4, 4);
           if(strpos($ext,'.') === false) // jeigu tai folderis
           {
               return "    "; // grazinam biski tarpu kad rusiavimas butu ciki,
                             // susirusiuoja - folderiai virsuje
           }
           return $ext; // jei tai failas grazinam jo pletini
}



// grazina failus is nurodytos direktorijos ir sukisa i masyva
function getFiles($path) {
   $files = array();
   $fileNames = array();
   $i = 0;
   if (is_dir($path)) {
       if ($dh = opendir($path)) {
           while (($file = readdir($dh)) !== false) {
               if (($file == ".") || ($file == "..")) continue;
               $fullpath = $path . "/" . $file;
               //$fkey = strtolower($file);
               $fkey = $file;
               while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
               $a = stat($fullpath);
               $files[$fkey]['size'] = $a['size'];
               if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
               else if ($a['size'] > 1024 && $a['size'] <= 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; //patvarkom failo dydziu atvaizdavima
               else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
               else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
               $files[$fkey]['name'] = $file;
               $e = strip_ext($file); // $e failo pletinys - pvz: .gif
               $files[$fkey]['type'] = filetype($fullpath); // failo tipas, dir, file ir pan
               $k=$e.$file; // kad butu lengvau rusiuoti;
               $fileNames[$i++] = $k;
           }
           closedir($dh);
       }
		else die (klaida("KLAIDA","Negalime nuskaityti direktorijos:  $path"));
   }
   else die (klaida("KLAIDA","Tai ne direktorija:  $path"));
   sort($fileNames,SORT_STRING); // surusiuojam
   $sortedFiles = array();
   $i = 0;
   foreach($fileNames as $f) {
           $f = substr($f, 4, strlen($f)-4); //sutvarko failo pletinius
           if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];   
    }
   return $sortedFiles;
}


// Navigacija
$startdir = "./";
if(isset($url['d'])) {
    $prev = $url['d'];
    $folder = $url['d'];   
} 
else { $folder = $startdir; $prev='';}
// END navigacija

$files = getFiles($folder);
$return = '';
foreach ($files as $file) {
    //if(strip_ext($file['name'])!='htaccess') {
    if($file['name']!='.htaccess') {	//Jeigu tai nera htaccess failas
        $image = $img_file;
        $extra = "<a href=\"".url('v,'.$file['name'].'')."\"><img src=\"$img_surce\" border=\"0\"/></a>";
        if($file['type']=='dir') {	//jeigu direktorija
            $image = $img_folder;
            $extra = "";
            $cmd='?id,46;a,8;d,'.$prev.$file['name'].'/';
        }
        else $cmd=$prev.$file['name'];
        $return .= "$extra <a href=\"$cmd\" title=\"".$file['type'].", ".$file['sizetext']."\"><img src=\"$image\" border=\"0\" /> ".$file['name']."</a> <br/>";
    }
}
if (isset($url['d'])) {
	$folder = explode("/",$url['d']);
	$link = ''; $dir = '';
	foreach ($folder as $fname) {
		$dir .= $fname."/";
		$link .= " <a href=\"?id,46;a,8;d,$dir\"\">$fname</a>";
	}
}
lentele("<a href=\"?id,46;a,8\">Failai</a>: ".((!empty($link))?$link:'')."", $return);

?>